<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Using custom scalar types</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('TopicCustomizing_CustomScalar.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Using custom scalar types </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="user_defined_scalars"></a> By default, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> currently supports standard floating-point types (<code>float</code>, <code>double</code>, <code>std::complex&lt;float&gt;</code>, <code>std::complex&lt;double&gt;</code>, <code>long</code> <code>double</code>), as well as all native integer types (e.g., <code>int</code>, <code>unsigned</code> <code>int</code>, <code>short</code>, etc.), and <code>bool</code>. On x86-64 systems, <code>long</code> <code>double</code> permits to locally enforces the use of x87 registers with extended accuracy (in comparison to SSE).</p>
<p>In order to add support for a custom type <code>T</code> you need:</p><ol type="1">
<li>make sure the common operator (+,-,*,/,etc.) are supported by the type <code>T</code> </li>
<li>add a specialization of struct Eigen::NumTraits&lt;T&gt; (see <a class="el" href="structEigen_1_1NumTraits.html">NumTraits</a>)</li>
<li>define the math functions that makes sense for your type. This includes standard ones like sqrt, pow, sin, tan, conj, real, imag, etc, as well as abs2 which is <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> specific. (see the file <a class="el" href="MathFunctions_8h_source.html">Eigen/src/Core/MathFunctions.h</a>)</li>
</ol>
<p>The math function should be defined in the same namespace than <code>T</code>, or in the <code>std</code> namespace though that second approach is not recommended.</p>
<p>Here is a concrete example adding support for the Adolc's <code>adouble</code> type. <a href="https://projects.coin-or.org/ADOL-C">Adolc</a> is an automatic differentiation library. The type <code>adouble</code> is basically a real value tracking the values of any number of partial derivatives.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#ifndef ADOLCSUPPORT_H</span></div>
<div class="line"><span class="preprocessor">#define ADOLCSUPPORT_H</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#define ADOLC_TAPELESS</span></div>
<div class="line"><span class="preprocessor">#include &lt;adolc/adouble.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Core&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span><a class="code" href="namespaceEigen.html">Eigen</a> {</div>
<div class="line"> </div>
<div class="line"><span class="keyword">template</span>&lt;&gt; <span class="keyword">struct </span>NumTraits&lt;adtl::adouble&gt;</div>
<div class="line"> : NumTraits&lt;double&gt; <span class="comment">// permits to get the epsilon, dummy_precision, lowest, highest functions</span></div>
<div class="line">{</div>
<div class="line">  <span class="keyword">typedef</span> adtl::adouble Real;</div>
<div class="line">  <span class="keyword">typedef</span> adtl::adouble NonInteger;</div>
<div class="line">  <span class="keyword">typedef</span> adtl::adouble Nested;</div>
<div class="line"> </div>
<div class="line">  <span class="keyword">enum</span> {</div>
<div class="line">    IsComplex = 0,</div>
<div class="line">    IsInteger = 0,</div>
<div class="line">    IsSigned = 1,</div>
<div class="line">    RequireInitialization = 1,</div>
<div class="line">    ReadCost = 1,</div>
<div class="line">    AddCost = 3,</div>
<div class="line">    MulCost = 3</div>
<div class="line">  };</div>
<div class="line">};</div>
<div class="line"> </div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span>adtl {</div>
<div class="line"> </div>
<div class="line"><span class="keyword">inline</span> <span class="keyword">const</span> adouble&amp; <a class="code" href="namespaceEigen.html#ab84f39a06a18e1ebb23f8be80345b79d">conj</a>(<span class="keyword">const</span> adouble&amp; x)  { <span class="keywordflow">return</span> x; }</div>
<div class="line"><span class="keyword">inline</span> <span class="keyword">const</span> adouble&amp; <a class="code" href="namespaceEigen.html#ac74dc920119b1eba45e9218d9f402afc">real</a>(<span class="keyword">const</span> adouble&amp; x)  { <span class="keywordflow">return</span> x; }</div>
<div class="line"><span class="keyword">inline</span> adouble <a class="code" href="namespaceEigen.html#a04d60a3c8a266f63c08e03615c1985c9">imag</a>(<span class="keyword">const</span> adouble&amp;)    { <span class="keywordflow">return</span> 0.; }</div>
<div class="line"><span class="keyword">inline</span> adouble <a class="code" href="namespaceEigen.html#ae27242789e7e62a8c42579b79be59b1a">abs</a>(<span class="keyword">const</span> adouble&amp;  x)  { <span class="keywordflow">return</span> fabs(x); }</div>
<div class="line"><span class="keyword">inline</span> adouble <a class="code" href="namespaceEigen.html#a54cc34b64b4935307efc06d56cd531df">abs2</a>(<span class="keyword">const</span> adouble&amp; x)  { <span class="keywordflow">return</span> x*x; }</div>
<div class="line"> </div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#endif </span><span class="comment">// ADOLCSUPPORT_H</span></div>
<div class="ttc" id="anamespaceEigen_html"><div class="ttname"><a href="namespaceEigen.html">Eigen</a></div><div class="ttdoc">Namespace containing all symbols from the Eigen library.</div><div class="ttdef"><b>Definition:</b> Core:139</div></div>
<div class="ttc" id="anamespaceEigen_html_a04d60a3c8a266f63c08e03615c1985c9"><div class="ttname"><a href="namespaceEigen.html#a04d60a3c8a266f63c08e03615c1985c9">Eigen::imag</a></div><div class="ttdeci">const Eigen::CwiseUnaryOp&lt; Eigen::internal::scalar_imag_op&lt; typename Derived::Scalar &gt;, const Derived &gt; imag(const Eigen::ArrayBase&lt; Derived &gt; &amp;x)</div></div>
<div class="ttc" id="anamespaceEigen_html_a54cc34b64b4935307efc06d56cd531df"><div class="ttname"><a href="namespaceEigen.html#a54cc34b64b4935307efc06d56cd531df">Eigen::abs2</a></div><div class="ttdeci">const Eigen::CwiseUnaryOp&lt; Eigen::internal::scalar_abs2_op&lt; typename Derived::Scalar &gt;, const Derived &gt; abs2(const Eigen::ArrayBase&lt; Derived &gt; &amp;x)</div></div>
<div class="ttc" id="anamespaceEigen_html_ab84f39a06a18e1ebb23f8be80345b79d"><div class="ttname"><a href="namespaceEigen.html#ab84f39a06a18e1ebb23f8be80345b79d">Eigen::conj</a></div><div class="ttdeci">const Eigen::CwiseUnaryOp&lt; Eigen::internal::scalar_conjugate_op&lt; typename Derived::Scalar &gt;, const Derived &gt; conj(const Eigen::ArrayBase&lt; Derived &gt; &amp;x)</div></div>
<div class="ttc" id="anamespaceEigen_html_ac74dc920119b1eba45e9218d9f402afc"><div class="ttname"><a href="namespaceEigen.html#ac74dc920119b1eba45e9218d9f402afc">Eigen::real</a></div><div class="ttdeci">const Eigen::CwiseUnaryOp&lt; Eigen::internal::scalar_real_op&lt; typename Derived::Scalar &gt;, const Derived &gt; real(const Eigen::ArrayBase&lt; Derived &gt; &amp;x)</div></div>
<div class="ttc" id="anamespaceEigen_html_ae27242789e7e62a8c42579b79be59b1a"><div class="ttname"><a href="namespaceEigen.html#ae27242789e7e62a8c42579b79be59b1a">Eigen::abs</a></div><div class="ttdeci">const Eigen::CwiseUnaryOp&lt; Eigen::internal::scalar_abs_op&lt; typename Derived::Scalar &gt;, const Derived &gt; abs(const Eigen::ArrayBase&lt; Derived &gt; &amp;x)</div></div>
</div><!-- fragment --><p>This other example adds support for the <code>mpq_class</code> type from <a href="https://gmplib.org/">GMP</a>. It shows in particular how to change the way <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> picks the best pivot during LU factorization. It selects the coefficient with the highest score, where the score is by default the absolute value of a number, but we can define a different score, for instance to prefer pivots with a more compact representation (this is an example, not a recommendation). Note that the scores should always be non-negative and only zero is allowed to have a score of zero. Also, this can interact badly with thresholds for inexact scalar types.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;gmpxx.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Core&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;boost/operators.hpp&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span><a class="code" href="namespaceEigen.html">Eigen</a> {</div>
<div class="line">  <span class="keyword">template</span>&lt;&gt; <span class="keyword">struct </span>NumTraits&lt;mpq_class&gt; : GenericNumTraits&lt;mpq_class&gt;</div>
<div class="line">  {</div>
<div class="line">    <span class="keyword">typedef</span> mpq_class Real;</div>
<div class="line">    <span class="keyword">typedef</span> mpq_class NonInteger;</div>
<div class="line">    <span class="keyword">typedef</span> mpq_class Nested;</div>
<div class="line"> </div>
<div class="line">    <span class="keyword">static</span> <span class="keyword">inline</span> Real epsilon() { <span class="keywordflow">return</span> 0; }</div>
<div class="line">    <span class="keyword">static</span> <span class="keyword">inline</span> Real dummy_precision() { <span class="keywordflow">return</span> 0; }</div>
<div class="line">    <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">int</span> digits10() { <span class="keywordflow">return</span> 0; }</div>
<div class="line"> </div>
<div class="line">    <span class="keyword">enum</span> {</div>
<div class="line">      IsInteger = 0,</div>
<div class="line">      IsSigned = 1,</div>
<div class="line">      IsComplex = 0,</div>
<div class="line">      RequireInitialization = 1,</div>
<div class="line">      ReadCost = 6,</div>
<div class="line">      AddCost = 150,</div>
<div class="line">      MulCost = 100</div>
<div class="line">    };</div>
<div class="line">  };</div>
<div class="line"> </div>
<div class="line">  <span class="keyword">namespace </span>internal {</div>
<div class="line"> </div>
<div class="line">    <span class="keyword">template</span>&lt;&gt; <span class="keyword">struct </span>scalar_score_coeff_op&lt;mpq_class&gt; {</div>
<div class="line">      <span class="keyword">struct </span>result_type : boost::totally_ordered1&lt;result_type&gt; {</div>
<div class="line">        std::size_t len;</div>
<div class="line">        result_type(<span class="keywordtype">int</span> i = 0) : len(i) {} <span class="comment">// Eigen uses Score(0) and Score()</span></div>
<div class="line">        result_type(mpq_class <span class="keyword">const</span>&amp; q) :</div>
<div class="line">          len(mpz_size(q.get_num_mpz_t())+</div>
<div class="line">              mpz_size(q.get_den_mpz_t())-1) {}</div>
<div class="line">        <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator&lt;(result_type x, result_type y) {</div>
<div class="line">          <span class="comment">// 0 is the worst possible pivot</span></div>
<div class="line">          <span class="keywordflow">if</span> (x.len == 0) <span class="keywordflow">return</span> y.len &gt; 0;</div>
<div class="line">          <span class="keywordflow">if</span> (y.len == 0) <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line">          <span class="comment">// Prefer a pivot with a small representation</span></div>
<div class="line">          <span class="keywordflow">return</span> x.len &gt; y.len;</div>
<div class="line">        }</div>
<div class="line">        <span class="keyword">friend</span> <span class="keywordtype">bool</span> operator==(result_type x, result_type y) {</div>
<div class="line">          <span class="comment">// Only used to test if the score is 0</span></div>
<div class="line">          <span class="keywordflow">return</span> x.len == y.len;</div>
<div class="line">        }</div>
<div class="line">      };</div>
<div class="line">      result_type operator()(mpq_class <span class="keyword">const</span>&amp; x)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> x; }</div>
<div class="line">    };</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
